package cn.u313.service;

import cn.u313.comm.conststring.YuString;
import cn.u313.comm.eunm.Type;
import cn.u313.comm.utils.TxtUtil;
import cn.u313.comm.utils.Util;
import cn.u313.entry.JiLuEntry;
import cn.u313.entry.UserEntry;
import cn.u313.entry.YanYuEntry;
import cn.u313.repository.JiLuEntryRepository;
import cn.u313.repository.UserEntryRepository;
import cn.u313.repository.YanYuEntryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Transient;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service
public class YanYuEntryService {
    int numberChiYu = 5;
    @Autowired
    YanYuEntryRepository yanYuEntryRepository;
    @Autowired
    UserEntryRepository userEntryRepository;
    @Autowired
    JiLuEntryRepository jiLuEntryRepository;



//    @Transactional
    public String pay(String qq, String group) {
        YanYuEntry yanYuEntry0 = yanYuEntryRepository.byQQ(qq);
        if(yanYuEntry0 != null)
            return "你个渣男，竟然在外面有鱼了！";
        UserEntry userEntry = userEntryRepository.byQQ(qq);
        if(userEntry == null)
            return "请先签到获取悦币哦！";
        else if(userEntry.getM() < 200)
            return "悦币不足哦！购买嘤鱼需要 【200悦币】 哦！";
        YanYuEntry yanYuEntry  = new YanYuEntry();

        yanYuEntry.setExp(200);
        yanYuEntry.setQq(qq);
        yanYuEntry.setTime(Util.getDate());
        yanYuEntry.setYingBi(0);
        yanYuEntry.setGroupNumber(group);
        userEntry.payMe(-200);

        JiLuEntry jiLuEntry = new JiLuEntry();
        jiLuEntry.setN(200);
        jiLuEntry.setQ0n(100);
        jiLuEntry.setQq0(qq);
        jiLuEntry.setTime(Util.getTime());
        jiLuEntry.setType(Type.吃鱼);
        jiLuEntry.setGroupNumber(group);
        jiLuEntryRepository.save(jiLuEntry);


        yanYuEntryRepository.save(yanYuEntry);
        userEntryRepository.save(userEntry);


        return "恭喜你获得一条可爱的嘤鱼\n"+yanYuEntry.toString();
    }

    @Transactional
    public String eat(String qq, String qq1, String group) {
        if(!StringUtils.hasLength(qq1))
            return "没有选择目标哦！";
        int dayQQ = jiLuEntryRepository.countQqToQq1(qq, qq1,Type.吃鱼);
        if(dayQQ > 0)
            return "今天已经吃过他的鱼了呢！";

        YanYuEntry yanYuEntry0 = yanYuEntryRepository.byQQ(qq);
        YanYuEntry yanYuEntry1 = yanYuEntryRepository.byQQ(qq1);
        if(yanYuEntry0 == null)
            return "你还没获得嘤鱼呢！";
        if(yanYuEntry1 == null)
            return "对方还没获得嘤鱼呢！";
        int dayNumber = jiLuEntryRepository.countByqqAndType(qq, Type.吃鱼);
        if(dayNumber > numberChiYu)
            return "嗝...，今天吃不下了哦！";

        if(yanYuEntry1.getExp()<200)
           return ("对方经验小于200，有段位保护哦");

        if(yanYuEntry0.getExp()<200)
            return "成长值低于 200 处于宝宝期哦，不允许吃别的鱼（你吃不过）";

        double ca=(yanYuEntry0.getExp()*1.0/yanYuEntry1.getExp());
//        if(ca>3)
//            return ("对方鱼太小啦,还不够你塞牙缝，换别家鱼吧.\n[经验差值倍率为"+(int)(ca*100)/(100.0)+"]");
//        else if(ca > 2)
//            return ("你看小鱼儿那么可爱，不要欺负小鱼嘛.\n[经验差值倍率为"+(int)(ca*100)/(100.0)+"]");
//        else if(ca>1.5)
//           return ("对方鱼太小啦，比你的鱼灵活逃啦,换别家鱼吧.\n[经验差值倍率为"+(int)(ca*100)/(100.0)+"]");


        int number = yanYuEntry0.getExp() - yanYuEntry1.getExp();
        int n=0;
        if(number>0)
            n=(int)(Math.sqrt(number/30)+10)*100/200;
        else
            n=-((int)(Math.sqrt(-number/30)+10)*100/200);
        n = (int)(n*Math.random())+1;

        if(ca < 1){
            n = n>0?-n/2:n;
        }else if (ca>2){
            n=0;
        }


        if(Math.random()*100>50){

            n=n==0?1:n;
            yanYuEntry0.setChiYu(Util.intDate());
            yanYuEntry0.setExp(yanYuEntry0.getExp()+n);
            yanYuEntry1.setExp(yanYuEntry1.getExp()-n);

            JiLuEntry jiLuEntry = new JiLuEntry();
            jiLuEntry.setN(n);
            jiLuEntry.setQ0n(yanYuEntry0.getExp());
            jiLuEntry.setQq0(qq);
            jiLuEntry.setQq1(qq1);
            jiLuEntry.setTime(Util.getTime());
            jiLuEntry.setType(Type.吃鱼);
            jiLuEntry.setGroupNumber(group);
            jiLuEntryRepository.save(jiLuEntry);
            yanYuEntryRepository.save(yanYuEntry0);
            yanYuEntryRepository.save(yanYuEntry1);
            return (n>0?TxtUtil.radomTxtToF(YuString.successMsgs,n)+
                    yanYuEntry0: TxtUtil.radomTxtToF(YuString.failMsgs,-n)+yanYuEntry0);
        }else{
            JiLuEntry jiLuEntry = new JiLuEntry();
            jiLuEntry.setN(0);
            jiLuEntry.setQ0n(yanYuEntry0.getExp());
            jiLuEntry.setQq0(qq);
            jiLuEntry.setQq1(qq1);
            jiLuEntry.setTime(Util.getTime());
            jiLuEntry.setType(Type.吃鱼);
            jiLuEntry.setGroupNumber(group);
            jiLuEntryRepository.save(jiLuEntry);
            return TxtUtil.radomTxt(YuString.unknownMsgs);
        }


    }

    /**
     * 数据嘤鱼详情
     * @param qq
     * @param group
     * @return
     */
    public String get(String qq, String group) {
        YanYuEntry yanYuEntry = yanYuEntryRepository.byQQ(qq);
        if(yanYuEntry == null){
            return "鱼都没有，还查啥咧！";
        }
        return yanYuEntry.toString();
    }

    /**
     *
     * 投喂
     * @param qq
     * @param group
     * @param n
     * @return
     */
    @Transactional
    public String touWei(String qq, String group, int n) {
        UserEntry userEntry = userEntryRepository.byQQ(qq);
        if (userEntry == null)
            return "请先发送签到获得悦币哦！";

        YanYuEntry yanYuEntry = yanYuEntryRepository.byQQ(qq);
        if(yanYuEntry==null)
            return "欸? 你连鱼都没有，难道你想喂小嘤吗？";

        if(userEntry.getM() < n)
            return "悦币不足【"+n+"】哦！";
        yanYuEntry.setExp(yanYuEntry.getExp()+n);

        userEntry.payMe(-n);
        userEntryRepository.save(userEntry);
        yanYuEntryRepository.save(yanYuEntry);
        return "投喂成功，本次投喂【"+n+"】嘤料\n";
//        return "投喂成功，本次投喂【"+n+"】嘤料\n"+yanYuEntry.toString();
    }
    @Transactional
    public String maiYu(String qq, String group, int n) {
        YanYuEntry yanYuEntry = yanYuEntryRepository.byQQ(qq);
        if(yanYuEntry==null)
            return "欸? 你连鱼都没有，难道你想卖小嘤吗？";
        if(n>yanYuEntry.getExp()){
            return "欸? 你的鱼不值这个价，难道你想卖小嘤吗？";
        }
        //手续费
        int shouxu=n*3/1000;
        shouxu = shouxu==0?1:shouxu;
        UserEntry userEntry = userEntryRepository.byQQ(qq);
        yanYuEntry.setExp(yanYuEntry.getExp()-n);
        userEntry.payMe(n);//
        userEntry.payMe(-shouxu);//扣除手续
        JiLuEntry jiLuEntry = new JiLuEntry();
        jiLuEntry.setN(0);
        jiLuEntry.setQ0n(userEntry.getM());
        jiLuEntry.setQq0(qq);
        jiLuEntry.setQq1(null);
        jiLuEntry.setTime(Util.getTime());
        jiLuEntry.setType(Type.卖鱼);
        jiLuEntry.setGroupNumber(group);
        jiLuEntryRepository.save(jiLuEntry);
        yanYuEntryRepository.save(yanYuEntry);
        userEntryRepository.save(userEntry);
        return  "你的宝贝鱼儿又少了几斤肉\n扣除经验值:"+n+"\n兑换成"+n+"悦币\n手续费 "+shouxu+"悦币\n当前悦币数量："+userEntry.getM()+"\n嘤鱼成长值："+yanYuEntry.getExp();


    }

    /**
     * 嘤鱼等级
     * @param qq
     * @param group
     * @return
     */
    public String yYu(String qq, String group) {
        YanYuEntry yanYuEntry = yanYuEntryRepository.byQQ(qq);
        if (yanYuEntry == null) {
            return "没有查到【"+qq+"】的鱼哦！";
        }
        return yanYuEntry.toString();
    }
}
